<?php

namespace App\Logic;


use App\Model\UserMoneyLogModel;
use App\Model\UserMoneyModel;
use EasySwoole\ORM\AbstractModel;
use EasySwoole\ORM\Db\Cursor;
use EasySwoole\ORM\Db\CursorInterface;
use EasySwoole\ORM\Exception\Exception;
use Throwable;

class PayLogic
{
    /**
     * 添加金额到用户余额
     * @param int $userId
     * @param float $originalMoney
     * @param float $money
     * @param int $handType // 1:支付下单，2:充值余额
     * @param int $orderId
     * @return UserMoneyModel|array|bool|AbstractModel|Cursor|CursorInterface|null
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public static function rechargeMoney(int $userId = 0, float $originalMoney = 0.00, float $money = 0.00, int $handType = 1, int $orderId = 0)
    {
        $info = UserMoneyModel::create()->updateUserMoney($userId, [
            'balance' => ['inc', floatval($money)],
            'amount' => ['inc', floatval($money)],
            'update_time' => time(),
        ]);

        if ($handType == 2) {
            // 添加用户流水账记录
            (new UserMoneyLogModel())->add($userId, $originalMoney, $money, 7, $orderId, '充值');
        }

        return $info;
    }
}
